package com.yanqu.road.server.manager.activity.selectlottery;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.selectlottery.config.SelectLotteryConfig;
import com.yanqu.road.entity.activity.selectlottery.config.SelectLotteryGoodsConfig;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.DaoHelper;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.activity.apprentice.CrossApprenticeActivity;
import com.yanqu.road.utils.date.DateHelper;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossSelectLotteryMgr extends TempCrossMgr {

    /**
     * 活动MAP
     */
    private static Map<Integer, CrossSelectLotteryActivity> activityMap = new ConcurrentHashMap<>();

    public boolean removeExpireData() throws Exception {
        Set<Integer> acSet = new HashSet<>();
        long nowTime = System.currentTimeMillis();
        for (Integer activityId : activityMap.keySet()) {
            CrossSelectLotteryActivity activity = activityMap.get(activityId);
            // 展示期过3天后移除内存
            if (activity == null || activity.getConfig() == null || activity.getConfig().getActivityInfo() == null ||
                    nowTime > activity.getConfig().getActivityInfo().getEndShowTime() * 1000 + 3 * DateHelper.DAY_MILLIONS) {
                acSet.add(activityId);
            }
        }
        for (int activityId : acSet) {
            activityMap.remove(activityId);
        }
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        reloadActivityConfig();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        Map<Integer, ActivityInfo> activityInfoMap = ActivityMgr.getOpenCrossActivityMapByType(eActivityType.SelectLottery.getValue());
        //活动加载进内存
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            if (ActivityHelper.activityInShowTime(activityInfo)) {
                getActivity(activityInfo.getActivityId());
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        for (CrossSelectLotteryActivity activity : activityMap.values()) {
            try {
                activity.save();
            }catch (Exception e){
                getLogger().error("select lottery activity {} save err", activity.getActivityId(), e.getCause());
            }
        }
        return true;
    }

    /**
     * 获取活动
     * @param activityId
     * @return
     */
    public static CrossSelectLotteryActivity getActivity(int activityId) {
        CrossSelectLotteryActivity activity = activityMap.get(activityId);
        if (activity != null) {
            return activity;
        }
        //初始化活动
        return initActivity(activityId);
    }

    /**
     * 初始化活动
     * @param activityId
     * @return
     */
    private static CrossSelectLotteryActivity initActivity(int activityId) {
        synchronized (activityMap) {
            CrossSelectLotteryActivity activity = activityMap.get(activityId);
            if (activity != null) {
                return activity;
            }
            //读配置 获取活动信息 保证有活动信息
            ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
            if (activityInfo == null) {
                return null;
            }
            //不在展示期（+3天）也不处理了
            if (!ActivityHelper.activityInShowTime(activityInfo)) {
                return null;
            }
            //创建配置
            SelectLotteryConfig config = createConfig(activityInfo);
            //创建活动
            activity = new CrossSelectLotteryActivity(config);
            activityMap.put(activityId, activity);
            return activity;
        }
    }

    /**
     * create活动配置
     * @param activityInfo
     * @return
     */
    public static SelectLotteryConfig createConfig(ActivityInfo activityInfo) {
        //获取活动config
        List<Integer> activityIdList = new ArrayList<>();
        activityIdList.add(activityInfo.getActivityId());
        //获取config
        Map<String, ActivityConfig> configMap = ActivityBussiness.getActivityConfigMap(activityIdList).get(activityInfo.getActivityId());
        //奖励配置
        Map<Integer, SelectLotteryGoodsConfig> selectLotteryGoodsConfigMap = DaoHelper.getGenericConfigDaoImpl().getGenericConfigMap(activityInfo.getActivityId(), SelectLotteryGoodsConfig.class);
        //实例化活动配置
        return new SelectLotteryConfig(activityInfo, configMap, selectLotteryGoodsConfigMap);
    }

    /**
     * 刷新配置
     */
    public static synchronized void reloadActivityConfig() {
        getLogger().info("CrossSelectLotteryMgr reloadConfig start");
        for (CrossSelectLotteryActivity activity : activityMap.values()) {
            //获取一下新的ActivityInfo
            ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activity.getActivityId());
            if (activityInfo == null) {
                continue;
            }
            //构造活动配置
            SelectLotteryConfig newConfigData = createConfig(activityInfo);
            //刷新配置信息
            activity.refreshConfig(newConfigData);
            getLogger().info("CrossSelectLotteryMgr reloadConfig success,activityId:{}", activityInfo.getActivityId());
        }
        getLogger().info("CrossSelectLotteryMgr reloadConfig end");
    }
}
